package com.sso.util;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.xml.sax.InputSource;

import com.base.commons.SpringBeanHolder;
import com.sso.entity.DicHuman;
import com.sso.entity.DicSystem;
import com.sso.entity.DicSystemUser;
import com.sso.entity.DicUserHuman;
import com.sso.service.IDicHumanService;
import com.sso.service.IDicSystemService;
import com.sso.service.IDicSystemUserService;
import com.sso.service.IDicUserHumanService;

public class ServiceParse {
	private IDicHumanService dicHumanService = SpringBeanHolder.
	getBean("dicHumanService");
	
	private IDicSystemUserService dicSystemUserService = SpringBeanHolder
	.getBean("dicSystemUserService");
	
	private IDicUserHumanService dicUserHumanService = SpringBeanHolder.
	getBean("dicUserHumanService");
	
	private IDicSystemService dicSystemService = SpringBeanHolder.
	getBean("dicSystemService");
	
	List<DicHuman> chList = new ArrayList<DicHuman>();
	
	public String parseRoot(String xml)
	{
		//结果信息
		String successinfo = null;
		//结果类型
		String returnType = null;
		
		//结果类型
		String resultType = null;
		//结果信息
	    String resultInfo = null;
	    //用户信息
	    String userInfo = null;
		//方法名
		String functionName = null;
		//系统
		DicSystem system = new DicSystem();
		//业务系统用户
		DicHuman human = new DicHuman();
		
		DicUserHuman humanUser = new DicUserHuman();
		//业务系统名称
		String sysName="";
		
		StringReader read = new StringReader(xml);

	    InputSource source = new InputSource(read);

	    SAXBuilder sb = new SAXBuilder();
	    
	    try{
			 Document doc = sb.build(source);

		     Element root = doc.getRootElement();
		      
		     List list = root.getChildren();
		      
		     if (list.size() > 0) {
		    	 for (int i = 0; i < list.size(); i++) {
		    		 Element element = (Element)list.get(i);
		             String tagName = element.getName();
		             
		             if ("ResultType".equals(tagName)) {
		                 resultType = element.getText();
		               }

		               if ("ResultInfo".equals(tagName)) {
		                 resultInfo = element.getText();
		               }

		               if ("UserInfo".equals(tagName)) {
		                 List sysList = element.getChildren();

		                 for (int x = 0; x < sysList.size(); x++) {
		                   Element sysElement = (Element)sysList.get(x);

		                   sysName = sysElement.getName();
		                 }
		             
		             if ("function".equals(tagName)) {
		                 functionName = element.getAttributeValue("name");
		             }

		             if ("params".equals(tagName)) {
		               List childList = element.getChildren();

		               for (int j = 0; j < childList.size(); j++) {
		                 Element childElement = (Element)childList.get(j);
		                 String childName = childElement.getName();

		                 if (("HumanCode".equals(childName)) && 
		                   (!"".equals(childElement.getText())) && (childElement.getText() != null))
		                 {
		                   human.setHumanName(childElement.getText());
		                 }

		                 if (("Password".equals(childName)) && 
		                   (!"".equals(childElement.getText())) && (childElement.getText() != null)) {
		                   //user.setUserpass(childElement.getText());
		                   human.setHumanPassword(childElement.getText());
		                 }

		                 if ("SystemID".equals(childName))
		                 {
		                     sysName  = childElement.getText();
		                 }
		                
		               }

		             }

		           }
		    	 }
		     }
		     if(functionName.equals("login")){
		    	 if(human!=null){
		    		 return human.getHumanName()+"##"+human.getHumanPassword();
		    	 }
		     }
		     
		     if(functionName.equals("getHuman")){
		    	 UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
		    	    .getAuthentication()
		    	    .getPrincipal();
		    	 String userName = userDetails.getUsername();
		    	 
		    	 DicSystemUser user = dicSystemUserService.getByUserName(userName).get(0);
		    	 
		    	 if(!"".equals(sysName)){
		    		if(sysName.equals("epm")){
		    			sysName = "预案管理系统";
		    		}
		    	 system =  dicSystemService.getBySystem(sysName).get(0);
		    	 }
		    	 humanUser = dicUserHumanService.getUserHumanBySystemId(user.getId(), system.getId()).get(0);
		    	 
		    	 human = humanUser.getHuman();
		    	 
		    	 if(!"".equals(human.getHumanName()) && !"".equals(human.getHumanPassword())){
		    	 return new String(" <?xml version=\"1.0\" encoding=\"gb2312\"?><request>" +
		    	 		"<function name=\"login\"/><params><HumanCode>"+human.getHumanName()+
		    	 		"</HumanCode><Password >"+human.getHumanPassword()+"</Password ></params></request>");
		    	 }
		     }
		        
			
		}catch(Exception ee){
			
		}
		 returnType = "0";
    	 successinfo = "获取用户不成功！";
   	 String request = new String("<?xml version=\"1.0\" encoding=\"gb2312\"?><Result><ResultType>" + 
   		      returnType + "</ResultType>" + "<ResultInfo>" + successinfo + "</ResultInfo></Result>");
   	 return request;
	    
	}
}
